/**
 * 判断url是否是http或https
 * @param {string} path
 * @returns {Boolean}
 */
export const isHttp = (url: string | string[]) => {
  return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
}

/**
 * 判断path是否为外链
 * @param {string} path
 * @returns {Boolean}
 */
export const isExternal = (path: string) => {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/** 是否是字符串
 * @param {string} str
 * @returns {Boolean}
 */
export const isString = (str: any) => {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/** 是否是数组
 * @param {Array} arg
 * @returns {Boolean}
 */
export const isArray = (arg: any) => {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
/** 校验手机号
 * @param {string} 手机号
 * @returns {Boolean}
 */
export const validatePhone = (rule: any, value: string, callback: (arg0?: Error | undefined) => void) => {
  const phoneReg = /^[1][3,4,5,7,8,9][0-9]{9}$/
  if (!value) {
    callback()
  }
  if (!phoneReg.test(value)) {
    callback(new Error('请输入正确手机号'))
  } else {
    callback()
  }
}
// 校验账号
export const vlidateUser = async (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback()
  }
  const reg = /^[a-zA-Z0-9]{1,16}$/
  if (!reg.test(value)) {
    return callback(new Error('只能输入字母和数字'))
  } else {
    return callback()
  }
}
// 校验邮箱
export const validateEmail = (rule: any, value: string, callback: (arg0?: Error | undefined) => void) => {
  const reg = /^([a-zA-Z]|[0-9])(\w|-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/
  if (value) {
    if (!reg.test(value)) {
      callback(new Error('请输入正确的邮箱'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}
// 校验真实姓名
export const vlidateRealName = async (rule: any, value: any, callback: any) => {
  if (!value) {
    callback()
  }
  if (value.length > 30) {
    callback(new Error('真实姓名不允许超过30个字符'))
  } else {
    callback()
  }
}
// 校验编码
export const validCode = (rule: any, value: any, callback: any) => {
  const reg = /^[0-9a-zA-Z]*$/g
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '编码只能由字母和数字组成'))
    } else if (value.length > 64) {
      callback(new Error('字符长度不得超过64位'))
    } else {
      callback()
    }
  }
}
// 校验中文字母数字组成
export const validName = (rule: any, value: any, callback: any) => {
  const reg = /^[0-9a-zA-Z\u4E00-\u9FA5]*$/g
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '名称只能由中文/字母/数字组成'))
    } else if (value.length > 64) {
      callback(new Error('字符长度不得超过64位'))
    } else {
      callback()
    }
  }
}
// 校验英文字母特殊字符组成
export const validMenu = (rule: any, value: any, callback: any) => {
  const reg = /^[0-9a-zA-Z/?=]*$/g
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '只能输入字母/数字/特殊字符'))
    } else {
      callback()
    }
  }
}
// 校验权限字符
export const validPermissionKey = (rule: any, value: any, callback: any) => {
  const reg = /^[0-9a-zA-Z:]*$/g
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '只能输入字母/数字/冒号'))
    } else {
      callback()
    }
  }
}
// 校验价格
export const validPrice = (rule: any, value: any, callback: any) => {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]{1,2}?$)/
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '价格必须大于0,可保留两位小数'))
    } else {
      callback()
    }
  }
}
// 校验4位价格
export const validPriceFour = (rule: any, value: any, callback: any) => {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,4})?$)|(^(0){1}$)|(^[0-9]\.[0-9]{1,4}?$)/
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '价格必须大于0,可保留四位小数'))
    } else {
      callback()
    }
  }
}
// 校验正整数
export const validInitNum = (rule: any, value: any, callback: any) => {
  const reg = /(^[1-9]\d*$)/
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '只能输入正整数'))
    } else {
      callback()
    }
  }
}
// 校验英文
export const validEnglish = (rule: any, value: any, callback: any) => {
  const reg = /^[a-zA-Z]*$/g
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error(rule?.message || '只能输入英文'))
    } else {
      callback()
    }
  }
}
// 校验百分数
export const validTaxRate = (rule: any, value: any, callback: any) => {
  const reg = /^((?!0)\d{1,2}|100|0)$/
  if (!value) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error('只能输入0-100的整数'))
    } else {
      callback()
    }
  }
}
